package com.xixibaby.core.login;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import com.xixibaby.core.dict.DictMapping;
import lombok.Getter;
import lombok.Setter;

/**
 * 用户的登录信息
 */
@Getter
@Setter
public class LoginInfoVO {
    @DictMapping("APP_SOURCE")
    private Integer platform;

    private String sessionId;
    //账户
    private Long userId;

    private static final AES AES;

    static {
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(),"TWF2020YES1956B1".getBytes()).getEncoded();
        AES = SecureUtil.aes(key);
    }

    @Override
    public String toString() {
        String format = StrUtil.format("{}_{}_{}", platform, sessionId, userId);
        return AES.encryptBase64(format);
    }

    public static LoginInfoVO toVO(String vostr) {
        vostr = new String(AES.decrypt(vostr));
        String[] split = vostr.split("_");
        LoginInfoVO loginInfoVO = new LoginInfoVO();
        loginInfoVO.setPlatform(Integer.valueOf(split[0]));
        loginInfoVO.setSessionId(split[1]);
        loginInfoVO.setUserId(Long.valueOf(split[2]));
        return loginInfoVO;
    }


}
